package com.lincolnXmypcqq.robot.framework;

import com.lincolnXmypcqq.cmds.framework.RobotCmd;
import com.lincolnXmypcqq.cmds.framework.RobotCmdExecuteResult;
import com.lincolnXmypcqq.robot.RobotDeal;

import java.util.*;

/**
 * @author ziyao.peng01@ucarinc.com
 * @description 消息处理上下文(因为整体设计是基于一条消息一个线程的方式去做的)
 * @date 2018/11/29 9:52
 **/
public class RobotDealContext {
    private static ThreadLocal<RobotDealContext> robotDealContextThreadLocal = new ThreadLocal<>();

    /**
     * @param
     * @return java.lang.String
     * @description 取得当前上下文key
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:00
     */
    public static String getPrimaryKey() {
        return String.valueOf(Thread.currentThread().getId());
    }

    /**
     * @param
     * @return com.lincoln.robot.framework.RobotDealContext
     * @description 获取当前上下文
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:02
     */
    public static RobotDealContext getRobotDealContext() {
        return robotDealContextThreadLocal.get();
    }

    /**
     * @param robotQQ, msgType, subMsgType, from, fromQQ, toQQ, content, frame, point
     * @return void
     * @description 创建执行流程
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static void createRobotDeal(String robotQQ, Integer msgType, Integer subMsgType, String from, String fromQQ, String toQQ, String content, String frame, Integer point) {
        Map<String, Object> params = new HashMap<>(10);
        params.put("robotQQ", robotQQ);
        params.put("msgType", msgType);
        params.put("subMsgType", subMsgType);
        params.put("from", from);
        params.put("fromQQ", fromQQ);
        params.put("toQQ", toQQ);
        params.put("content", content);
        params.put("frame", frame);
        params.put("point", point);
        new RobotDeal(params);
    }

    public static RobotDeal getRobotDeal() {
        return robotDealContextThreadLocal.get().robotDeal;
    }

    public static void setRobotDeal(RobotDeal robotDeal) {
        robotDealContextThreadLocal.get().robotDeal = robotDeal;
    }

    /**
     * @param
     * @return void
     * @description 开始执行流程
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 11:39
     */
    public static void start() {
        robotDealContextThreadLocal.get().robotDeal.start();
    }

    /**
     * @param
     * @return void
     * @description 初始化上下文
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static void init(Map<String, Object> params) {
        robotDealContextThreadLocal.set(new RobotDealContext());
        setParams(params);
    }

    /**
     * @param robotDealContext
     * @return void
     * @description 设置当前上下文
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static void setRobotDealContext(RobotDealContext robotDealContext) {
        robotDealContextThreadLocal.set(robotDealContext);
    }

    /**
     * @param
     * @return void
     * @description 获取当前上下文缓存参数
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static Map<String, Object> getParams() {
        return robotDealContextThreadLocal.get().params;
    }

    /**
     * @param
     * @return void
     * @description 设置当前上下文缓存参数
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static void setParams(Map<String, Object> params) {
        robotDealContextThreadLocal.get().params = params;
    }

    /**
     * @param
     * @return void
     * @description 获取当前上下文缓存参数
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static <T> T getParam(String key) {
        return (T) robotDealContextThreadLocal.get().params.get(key);
    }

    /**
     * @param
     * @return void
     * @description 设置当前上下文缓存参数
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 10:05
     */
    public static void setParam(String key, Object value) {
        robotDealContextThreadLocal.get().params.put(key, value);
    }

    /**
     * @param robotCmd
     * @return void
     * @description 添加一个要执行的任务
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 11:22
     */
    public static void addRobotCmd(RobotCmd robotCmd) {
        robotDealContextThreadLocal.get().robotCmds.add(robotCmd);
    }

    /**
     * @param robotCmds
     * @return void
     * @description 添加一些要执行的任务
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 11:22
     */
    public static void addRobotCmds(Collection<RobotCmd> robotCmds) {
        robotDealContextThreadLocal.get().robotCmds.addAll(robotCmds);
    }

    /**
     * @param robotCmdExecuteResult
     * @return void
     * @description 添加一个结果
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 11:21
     */
    public static void addRobotCmdExecuteResult(RobotCmdExecuteResult robotCmdExecuteResult) {
        robotDealContextThreadLocal.get().robotCmdExecuteResults.add(robotCmdExecuteResult);
    }

    /**
     * @param robotCmdExecuteResults
     * @return void
     * @description 添加一些结果
     * @author ziyao.peng01@ucarinc.com
     * @date 2018/11/29 11:21
     */
    public static void addRobotCmdExecuteResults(Collection<RobotCmdExecuteResult> robotCmdExecuteResults) {
        robotDealContextThreadLocal.get().robotCmdExecuteResults.addAll(robotCmdExecuteResults);
    }

    public static String getRobotQQ() {
        return getParam("robotQQ");
    }

    public static void setRobotQQ(String robotQQ) {
        setParam("robotQQ", robotQQ);
    }

    public static Integer getMsgType() {
        return getParam("msgType");
    }

    public static void setMsgType(Integer msgType) {
        setParam("msgType", msgType);
    }

    public static Integer getSubMsgType() {
        return getParam("subMsgType");
    }

    public static void setSubMsgType(Integer subMsgType) {
        setParam("subMsgType", subMsgType);
    }

    public static String getFrom() {
        return getParam("from");
    }

    public static void setFrom(String from) {
        setParam("from", from);
    }

    public static String getFromQQ() {
        return getParam("fromQQ");
    }

    public static void setFromQQ(String fromQQ) {
        setParam("fromQQ", fromQQ);
    }

    public static String getToQQ() {
        return getParam("toQQ");
    }

    public static void setToQQ(String toQQ) {
        setParam("toQQ", toQQ);
    }

    public static String getContent() {
        return getParam("content");
    }

    public static void setContent(String content) {
        setParam("content", content);
    }

    public static String getFrame() {
        return getParam("frame");
    }

    public static void setFrame(String frame) {
        setParam("frame", frame);
    }

    public static Integer getPoint() {
        return getParam("point");
    }

    public static void setPoint(Integer point) {
        setParam("point", point);
    }

    //================================================================================================================

    /**
     * 需要执行的命令集
     */
    private List<RobotCmd> robotCmds = new ArrayList<>();
    /**
     * 执行过得命令集
     */
    private List<RobotCmdExecuteResult> robotCmdExecuteResults = new ArrayList<>();
    /**
     * 参数(可以被修改)
     */
    private Map<String, Object> params = new HashMap<>();
    /**
     * 执行流程
     */
    private RobotDeal robotDeal;
}
